/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.util.profiler;
import hapi.math.MathUtil;
import haxe.PosInfos;

class MemoryUsageMark {
	
	public var label(default,null):String;
	public var bytes(default, null):Float;
	public var posInfos(default, null):PosInfos;
	public var megaBytes(getMegaBytes, null):Float;
	
	public function new(label, bytes, posInfos) {
		this.label = label;
		this.bytes = bytes;
		this.posInfos = posInfos;
	}
	
	public function toSummaryString(prevUsageInBytes:Float):String {
		var buf = new StringBuf();
		var changeInBytes = bytes - prevUsageInBytes;
		buf.add(megaBytes);
		buf.add(" Mb ( ");
		buf.add(MathUtil.roundTo(changeInBytes,2));
		buf.add(" bytes ) ");
		buf.add(" : ");
		buf.add(label);
		if ( posInfos != null ) {
			buf.add(" <");
			buf.add(posInfos.className);
			buf.add(".");
			buf.add(posInfos.methodName);
			buf.add("[" + posInfos.lineNumber + " line]>\n");
		}else {
			buf.add("\n");
		}
		return buf.toString();
	}
	
	function getMegaBytes() {
		return MathUtil.roundTo(bytes/(1024*1024), 4);
	}
}
